package com.qunar.dyds.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qunar.base.meerkat.util.StringUtil;
import com.qunar.common.admin.service.AbstractService;
import com.qunar.dyds.dto.HomeVO;
import com.qunar.dyds.entity.Competitor;
import com.qunar.dyds.entity.CompetitorStatus;
import com.qunar.dyds.entity.InviteCode;
import com.qunar.dyds.entity.InviteCodeStatus;

/**
 * 导游信息服务
 */
@Service
public class CompetitorService extends AbstractService {
    //
    // @Resource
    // private CacheClientAdapter cache;

    @Resource
    private InviteCodeService inviteCodeService;

    /**
     * 注册用户
     * 
     * @param _competitor 注册用户信息
     * @param pCode 注册码信息
     * @return 更新数
     */
    @Transactional
    public int add(Competitor _competitor, InviteCode _code) {

        logger.debug("add Start");

        // 检测注册码（存在 | 状态 | 身份证匹配）
        if (StringUtil.isEmpty(_competitor.getInviteCode())) {
            throw new IllegalArgumentException("报名序号不能为空");
        }
        InviteCode code = inviteCodeService.get(_competitor.getInviteCode());
        if (code == null || !InviteCodeStatus.CREATED.equals(code.getStatus())
                || !code.getCertNo().equals(_code.getCertNo())) {
            throw new IllegalArgumentException("无效报名号");
        }

        // 更新报名序号状态
        InviteCode param = new InviteCode();
        param.setId(code.getId());
        param.setStatus(InviteCodeStatus.ACTIVED);
        param.setUpdated(new Date());
        inviteCodeService.update(param);

        // 添加注册
        int ret = updateDAO.execute("v_competitor.insert", _competitor);

        logger.debug("add End");

        return ret;
    }

    /**
     * 查询选手信息
     * 
     * @param id 选手ID
     * @return 选手信息
     */
    public Competitor get(int id) {

        logger.debug("get Start");

        // Competitor c = get(id, Setting.ENABLE_CACHE);

        Map<String, Object> bindParams = new HashMap<String, Object>();
        bindParams.put("id", id);

        Competitor competitor = queryDAO.executeForObject("v_competitor.select", bindParams, Competitor.class);

        logger.debug("get End.");

        return competitor;
    }

    /**
     * 获取参选人信息
     * 
     * @param id 参选人ID
     * @param useCache 是否使用缓存
     * @return 参选人信息
     */
    /*
     * public Competitor get(int id, boolean useCache) {
     * 
     * Competitor competitor = null; String key = "dyds_c_id_" + id;
     * 
     * if (useCache) { competitor = (Competitor) cache.get(key); }
     * 
     * if (competitor == null) { Map<String, Object> bindParams = new
     * HashMap<String, Object>(); bindParams.put("id", id);
     * 
     * competitor = queryDAO.executeForObject("v_competitor.get", bindParams,
     * Competitor.class); if (useCache && competitor != null) { cache.set(key,
     * competitor); } }
     * 
     * return competitor; }
     */

    /**
     * 查询选手信息
     * 
     * @param username 选手用户名
     * @return 选手信息
     */
    public Competitor get(String username) {

        logger.debug("get Start");

        if (StringUtil.isEmpty(username)) {
            throw new IllegalArgumentException("用户名不能为空");
        }

        // 查询选手信息
        Map<String, Object> bindParams = new HashMap<String, Object>();
        bindParams.put("username", username);
        Competitor competitor = queryDAO.executeForObject("v_competitor.select", bindParams, Competitor.class);

        logger.debug("get End.");

        return competitor;
    }

    /**
     * 获取票数最高的参赛人列表
     * 
     * @param limit
     * @param useCache
     * @return
     */
    public HomeVO getHomeVO() {

        logger.debug("getHomeVO Start.");

        HomeVO homeVO = null;
        List<Competitor> list = null;

        // String key = "dyds_c_top";
        // if (Setting.ENABLE_CACHE) {
        // list = (List<Competitor>) cache.get(key);
        // }

        Map<String, Object> cond = new HashMap<String, Object>();
        cond.put("status", CompetitorStatus.ACTIVED);
        cond.put("sort", "vote_count desc");

        // if (list == null) {
        // Cache miss
        list = search(cond, 0, 50);
        // if (Setting.ENABLE_CACHE) {
        // cache.set(key, list, Setting.CACHE_EXPIRE);
        // }
        homeVO = new HomeVO(list, list);
        // } else {
        // homeVO = new HomeVO(search(cond, 0, 50), search(cond, 0, 12));
        // }

        logger.debug("getTop End.");

        return homeVO;
    }

    /**
     * 查询符合条件记录数
     * 
     * @param cond 查询条件
     * @return 记录数
     */
    public Integer getCount(Map<String, Object> cond) {

        logger.debug("getCount Start.");

        Integer count = queryDAO.executeForObject("v_competitor.selectCount", cond, Integer.class);

        logger.debug("getCount End.");

        return count;
    }

    /**
     * 查询符合条件列表
     * 
     * @param cond 查询条件
     * @param startIndex 开始索引数
     * @param limit 查询条数
     * @return 结果列表
     */
    public List<Competitor> search(Map<String, Object> cond, int startIndex, int limit) {

        logger.debug("search Start.");

        List<Competitor> list = queryDAO.executeForObjectList("v_competitor.select", cond, startIndex, limit);

        logger.debug("search End");

        return list;
    }


    /**
     * 查询符合条件列表
     * 
     * @param cond 查询条件
     * @return 结果列表
     */
    public List<Competitor> search(Map<String, Object> cond) {

        logger.debug("search Start.");

        List<Competitor> list = queryDAO.executeForObjectList("v_competitor.select", cond);

        logger.debug("search End");

        return list;
    }

    /**
     * 更新选手信息
     * 
     * @param competitor 选手信息
     */
    @Transactional
    public int update(Competitor competitor) {

        logger.debug("update Start.");

        int ret = updateDAO.execute("v_competitor.update", competitor);

        logger.debug("update End.");

        return ret;
    }

    @Transactional
    public int vote(int id, int status) {

        logger.debug("vote Start.");

        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("id", id);
        params.put("status", status);

        int ret = updateDAO.execute("v_competitor.vote", params);

        logger.debug("vote End.");

        return ret;
    }

    /*
    @Transactional
    public int delete(int id) {

        logger.debug("delete Start.");

        int ret = updateDAO.execute("v_competitor.delete", id);

        logger.debug("delete End.");

        return ret;
    }*/
}
